﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {
        public string Comment()
        {
            string mystyle = this.Label_Inserted;
            string Option = this.GetParamValue("YX:Option");//文件名
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            switch (Option)
            {
                #region 点评表单
                case "CommentForm":
                    string CommentObjectId = string.Empty;
                    string CommentType = string.Empty;
                    string productId = ChangeHope.WebPage.PageRequest.GetQueryString("q_productid");
                    string storeId = ChangeHope.WebPage.PageRequest.GetQueryString("StoreId");
                    if (productId != "")
                    {
                        //商品点评
                        CommentObjectId = productId;
                        CommentType = "1";
                    }
                    else if(storeId!="")
                    {
                        //店铺点评
                        CommentObjectId = storeId;
                        CommentType = "2";
                    }
                    if (!string.IsNullOrEmpty(mystyle))
                    {
                        YXShop.BLL.Accessories.CommentForm cfbll = new YXShop.BLL.Accessories.CommentForm();
                        if (mystyle.IndexOf("[#form_Score]") > -1)
                        {
                            StringBuilder str = new StringBuilder();
                            str.Append("<span>评分：</span><input type=\"radio\" name=\"Rating\" value=\"5\" id=\"Rating\" checked=\"checked\" />5分");
                            str.Append("<input type=\"radio\" name=\"Rating\" value=\"4\" id=\"Rating\" />4分");
                            str.Append("<input type=\"radio\" name=\"Rating\" value=\"3\" id=\"Rating\" />3分");
                            str.Append("<input type=\"radio\" name=\"Rating\" value=\"2\" id=\"Rating\" />2分");
                            str.Append("<input type=\"radio\" name=\"Rating\" value=\"1\" id=\"Rating\" />1分");
                            str.Append("<input type=\"hidden\" name=\"CommentObjectId\" id=\"hiddenField\"  value=\"" + CommentObjectId + "\"/>");
                            str.Append("<input type=\"hidden\" name=\"CommentType\" id=\"hiddenField\"  value=\"" + CommentType + "\"/>");
                            mystyle = mystyle.Replace("[#form_Score]", str.ToString());
                        }
                        if (mystyle.IndexOf("[#form_title]") > -1)
                        {
                            mystyle = mystyle.Replace("[#form_title]", "<span>标题：</span><input name=\"txtTitle\" type=\"text\" /><span id=comTitle sytle='color:red;'>*</span>");
                        }
                        if (mystyle.IndexOf("[#form_lable]") > -1)
                        {
                            mystyle = mystyle.Replace("[#form_lable]", "<span>标签：</span><input name=\"txtTag\" type=\"text\"  /><span id=comTag sytle='color:red;'>*</span>");
                        }
                        if (mystyle.IndexOf("[#form_button]") > -1)
                        {
                            mystyle = mystyle.Replace("[#form_button]", "<input  name=\"bu\" type=\"submit\"  value=\"提交\" />");
                        }
                        ChangeHope.DataBase.DataByPage dataPage = cfbll.GetList();
                        if (dataPage.DataReader != null)
                        {
                            while (dataPage.DataReader.Read())
                            {
                                string CommentItemID = dataPage.DataReader["id"].ToString();
                                if (mystyle.IndexOf("[#CommentItem" + CommentItemID + "]") > -1)
                                {
                                    YXShop.Model.Accessories.CommentForm cfmodel = cfbll.GetModelID(int.Parse(dataPage.DataReader["id"].ToString()));
                                    switch (dataPage.DataReader["type"].ToString())
                                    {
                                        case "1":
                                            string selectStr = string.Empty;
                                            string[] arrselStr = dataPage.DataReader["datavalue"].ToString().Split('\n');
                                            selectStr = "<span>" + dataPage.DataReader["filed"].ToString() + "：</span>";
                                            selectStr += "<select name=\"txtCommentItem" + CommentItemID + "\">";
                                            for (int i = 0; i < arrselStr.Length; i++)
                                            {
                                                selectStr += "<option value=\"" + arrselStr[i].ToString() + "\">" + arrselStr[i].ToString() + "</option>";
                                            }
                                            selectStr += " </select>";
                                            selectStr += "<input type=\"hidden\" name=\"hfCommentItemID" + CommentItemID + "\" id=\"hiddenField\"  value=\"" + CommentItemID + "\"/>";
                                            mystyle = mystyle.Replace("[#CommentItem" + CommentItemID + "]", selectStr);
                                            break;
                                        case "2":
                                            string radio = string.Empty;
                                            string[] arrradioStr = dataPage.DataReader["datavalue"].ToString().Split('\n');
                                            radio = "<span>" + dataPage.DataReader["filed"].ToString() + "：</span>";
                                            for (int k = 0; k < arrradioStr.Length; k++)
                                            {
                                                radio += "<input type=\"radio\" name=\"txtCommentItem" + CommentItemID + "\" id=\"txtCommentItem" + CommentItemID + "\" />" + arrradioStr[k].ToString();
                                            }
                                            radio += "<input type=\"hidden\" name=\"hfCommentItemID" + CommentItemID + "\" id=\"hiddenField\"  value=\"" + CommentItemID + "\"/>";
                                            mystyle = mystyle.Replace("[#CommentItem" + CommentItemID + "]", radio);
                                            break;
                                        case "3":
                                            string checkbox = string.Empty;
                                            string[] arrStr = dataPage.DataReader["datavalue"].ToString().Split('\n');
                                            checkbox = "<span>" + dataPage.DataReader["filed"].ToString() + "：</span>";
                                            for (int f = 0; f < arrStr.Length; f++)
                                            {
                                                checkbox += "<input type=\"checkbox\" name=\"txtCommentItem" + CommentItemID + "\" id=\"txtCommentItem" + CommentItemID + "\" />" + arrStr[f].ToString();
                                            }
                                            checkbox += "<input type=\"hidden\" name=\"hfCommentItemID" + CommentItemID + "\" id=\"hiddenField\"  value=\"" + CommentItemID + "\"/>";
                                            mystyle = mystyle.Replace("[#CommentItem" + CommentItemID + "]", checkbox);
                                            break;
                                        case "4":
                                            string reStr = string.Empty;
                                            reStr = "<span>" + dataPage.DataReader["filed"].ToString() + "：</span>";
                                            reStr += "<input name=\"txtCommentItem" + CommentItemID + "\" type=\"text\" /><input type=\"hidden\" name=\"hfCommentItemID" + CommentItemID + "\" id=\"hiddenField\"  value=\"" + CommentItemID + "\"/>";
                                            mystyle = mystyle.Replace("[#CommentItem" + CommentItemID + "]", reStr);
                                            break;
                                        case "5":
                                            string reStr1 = string.Empty;
                                            reStr1 = "<span>" + dataPage.DataReader["filed"].ToString() + "：</span><br/>";
                                            reStr1 += "<textarea name=\"txtCommentItem" + CommentItemID + "\" id=\"textarea\" cols=\"45\" rows=\"5\"></textarea><input type=\"hidden\" name=\"hfCommentItemID" + CommentItemID + "\" id=\"hiddenField\"  value=\"" + CommentItemID + "\"/>";
                                            mystyle = mystyle.Replace("[#CommentItem" + CommentItemID + "]", reStr1);
                                            break;
                                    }

                                }
                            }
                        }
                    }

                    break;
                #endregion
                case "CommentInfo":
                    string str_ShowMode = this.GetParamValue("YX:ShowMode");//显示方式
                    string str_CommentLevel = this.GetParamValue("YX:CommentLevel");//评分等级
                    string str_CompositorType = this.GetParamValue("YX:CompositorType");//排列顺序
                    string str_CompositorField = this.GetParamValue("YX:CompositorField");//排列字段
                    string str_ShowRows = this.GetParamValue("YX:ShowRows");//显示行数
                    string str_ShowColumns = this.GetParamValue("YX:ShowColumns");//显示列数
                    string str_NameCharacter = this.GetParamValue("YX:NameCharacter");//标签长度
                    string str_WhetherPagination = this.GetParamValue("YX:WhetherPagination");//是否分页
                    string str_TempletLayout = this.GetParamValue("YX:TempletLayout");//布局方式
                    string str_ContentCharacter = this.GetParamValue("YX:ContentCharacter");//内容长度
                    string str_WhetherOmit = this.GetParamValue("YX:WhetherOmit");//是否省略
                    string strWhere = string.Empty;
                    StringBuilder shtml = new StringBuilder();
                    int Rows = 1;
                    if (!string.IsNullOrEmpty(str_ShowRows))
                    {
                        if (ChangeHope.Common.ValidateHelper.IsNumber(str_ShowRows))
                        {
                            Rows = Convert.ToInt32(str_ShowRows);
                        }
                    }

                    int Columns = 1;
                    if (!string.IsNullOrEmpty(str_ShowColumns))
                    {
                        if (ChangeHope.Common.ValidateHelper.IsNumber(str_ShowColumns))
                        {
                            Columns = Convert.ToInt32(str_ShowColumns);
                        }
                    }
                    //每页显示条数
                    int PageSize = Rows * Columns;
                    //标签长度
                    int NameLength = 8;
                    if (!string.IsNullOrEmpty(str_NameCharacter))
                    {
                        if (ChangeHope.Common.ValidateHelper.IsNumber(str_NameCharacter))
                        {
                            NameLength = Convert.ToInt32(str_NameCharacter);
                        }
                    }
                    //内容长度
                    int ContentLength = 8;
                    if (!string.IsNullOrEmpty(str_ContentCharacter))
                    {
                        if (ChangeHope.Common.ValidateHelper.IsNumber(str_ContentCharacter))
                        {
                            ContentLength = Convert.ToInt32(str_ContentCharacter);
                        }
                    }
                    //评分等级
                    switch (str_CommentLevel)
                    {
                        //全部
                        case "1":
                            strWhere += " and grade > 0 ";
                            break;
                        //好评
                        case "2":
                            strWhere += " and grade = 5 ";
                            break;
                        //中评
                        case "3":
                            strWhere += " and grade between 3 and 4 ";
                            break;
                        //差评
                        case "4":
                            strWhere += " and grade <= 2 ";
                            break;
                        default:
                            break;
                    }
                    //排序方式
                    string bystring = string.Empty;
                    if (str_CompositorType == "1")
                    {
                        bystring = " desc ";
                    }
                    else
                    {
                        bystring = " asc ";
                    }
                    //排序字段
                    string orderCom = " id ";
                    if (str_CompositorField == "CreateDate")
                    {
                        orderCom = " commenttime ";
                    }
                    string order = " [order by] " + orderCom + " " + bystring + "";
                    //显示方式
                    switch (str_ShowMode)
                    {
                        //全部的商品
                        case "1":
                            break;
                        //商品
                        case "2":
                                strWhere += " and type=1 ";
                            break;
                            //单条点评
                        case "3":
                            string CommentID = ChangeHope.WebPage.PageRequest.GetQueryString("q_commentid");
                            if (CommentID != "")
                            {
                                strWhere = " and id=" + CommentID + "";
                            }
                            break;
                            //店铺
                        case "4":
                                strWhere = " and type=2";
                            break;
                        //商品
                        case "5":
                            string product_Id = ChangeHope.WebPage.PageRequest.GetQueryString("q_productid");
                            strWhere += " and type=1 and commentid=" + product_Id + "";
                            break;
                        default:
                            break;
                    }

                    //没有记录数
                    if (!string.IsNullOrEmpty(mystyle))
                    {
                        YXShop.BLL.Accessories.CommentInfo cibll = new YXShop.BLL.Accessories.CommentInfo();
                        YXShop.BLL.Member.MemberAccount mabll = new YXShop.BLL.Member.MemberAccount();
                        YXShop.BLL.Member.MemberRank mrbll = new YXShop.BLL.Member.MemberRank();
                        ChangeHope.DataBase.DataByPage dataPage = cibll.GetList(order, PageSize, strWhere);
                        if (dataPage.DataReader != null)
                        {
                            int count = 0;
                            while (dataPage.DataReader.Read())
                            {
                                count++;
                                string ComID = dataPage.DataReader["id"].ToString();
                                string strContent = mystyle.ToString();
                                //查用户
                                string memberName = "游客";
                                string memberLevelImages = string.Empty;
                                string memberLevel = "游客";
                                YXShop.Model.Member.MemberAccount mamodel = mabll.GetModel(int.Parse(dataPage.DataReader["uid"].ToString()));
                                if (mamodel != null)
                                {
                                    memberName = mamodel.UserId;
                                    YXShop.Model.Member.MemberRank mrmodel = mrbll.GetModel(int.Parse(mamodel.UserGroup.ToString()));
                                    if (mrmodel != null)
                                    {
                                        memberLevelImages = sp.DummyPaht + mrmodel.LogoPic;
                                        memberLevel = mrmodel.Name;
                                    }
                                }

                                #region 商品信息
                                string productName = string.Empty;
                                    string productPrice = string.Empty;
                                    string productImagePic = string.Empty;
                                    string productIds = string.Empty;
                                if (str_ShowMode == "2")
                                {
                                    YXShop.BLL.Product.ProductInfo probll = new YXShop.BLL.Product.ProductInfo();
                                    YXShop.Model.Product.ProductInfo promodel = probll.GetModel(Convert.ToInt32(dataPage.DataReader["commentid"].ToString()));
                                    
                                    if (promodel != null)
                                    {
                                        productName = promodel.pro_Name;
                                        productPrice = promodel.pro_ShopPrice.ToString();
                                        productImagePic = promodel.pro_Thumbnail;
                                        productIds = promodel.pro_ID.ToString();
                                    }

                                }
                                if (strContent.IndexOf("[#Comment_Product_Images_Url]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comment_Product_Images_Url]", sp.DummyPaht+productImagePic);
                                }

                                if (strContent.IndexOf("[#Comment_Product_Name]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comment_Product_Name]", productName);
                                }

                                if (strContent.IndexOf("[#Comment_Product_Price]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comment_Product_Price]", productPrice);
                                }

                                if (strContent.IndexOf("[#Comment_Product_Url]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comment_Product_Url]", Common.UrlStr("productcontent", "q_productid=" + productIds + "", "product/"));
                                }
                                #endregion 商品信息结束

                                #region 商店信息
                                string storeName = string.Empty;
                                string storeImagePic = string.Empty;
                                string storeIds = string.Empty;
                                string storeUserId = string.Empty;
                                if (str_ShowMode == "4")
                                {
                                    YXShop.BLL.Shop.Shop shopbll = new YXShop.BLL.Shop.Shop();
                                    YXShop.Model.Shop.Shop shopmodel = shopbll.GetModelById(Convert.ToInt32(dataPage.DataReader["commentid"].ToString()));

                                    if (shopmodel != null)
                                    {
                                        storeName = shopmodel.Shopname;
                                        storeImagePic = shopmodel.Shopsign;
                                        storeIds = shopmodel.Sid.ToString();
                                        storeUserId = shopmodel.Uid.ToString();
                                    }

                                }
                                if (strContent.IndexOf("[#Comment_Store_Images_Url]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comment_Store_Images_Url]", sp.DummyPaht + storeImagePic);
                                }
                                if (strContent.IndexOf("[#Comment_Store_Name]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comment_Store_Name]", storeName);
                                }
                                if (strContent.IndexOf("[#Comment_Store_Url]") > -1)
                                {
                                    string storeUrl = Common.UrlStr("store_index", "StoreId=" + storeIds + ";UserId=" + storeUserId + "", "shop/");
                                    strContent = strContent.Replace("[#Comment_Store_Url]", storeUrl);
                                }
                                #endregion 商店信息结束

                                if (strContent.IndexOf("[#Comm_Count]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comm_Count]", count.ToString());
                                }
                                //会员头像图片//暂时不弄等级图片
                                if (strContent.IndexOf("[#Member_LevelPic]") > -1)
                                {
                                    if (string.IsNullOrEmpty(memberLevelImages))
                                    {
                                        memberLevelImages = sp.DummyPaht + "images/NaughtImages.gif";
                                    }
                                    strContent = strContent.Replace("[#Member_LevelPic]", "" + memberLevelImages + "");
                                }
                                //标题
                                if (strContent.IndexOf("[#Comm_Title]") > -1 && dataPage.DataReader["title"].ToString() != string.Empty && dataPage.DataReader["title"].ToString() != null)
                                {
                                    //是否显示省略号
                                    if (str_WhetherOmit == "True")
                                    {
                                        strContent = strContent.Replace("[#Comm_Title]", ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["title"].ToString(), NameLength, "..."));
                                    }
                                    else
                                    {
                                        strContent = strContent.Replace("[#Comm_Title]", ChangeHope.Common.StringHelper.SubString(dataPage.DataReader["title"].ToString(),NameLength));
                                    }

                                }
                                else
                                {
                                    strContent = strContent.Replace("[#Comm_Title]", "");
                                }
                                //评论时间
                                if (strContent.IndexOf("[#Comm_DateTime]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comm_DateTime]", dataPage.DataReader["commenttime"].ToString());
                                }
                                //回复数
                                if (strContent.IndexOf("[#Reply_Count]") > -1)
                                {
                                    strContent = strContent.Replace("[#Reply_Count]", "1");
                                }
                                //评论内容
                                if (strContent.IndexOf("[#Comm_Content]") > -1)
                                {
                                    string ContentList = dataPage.DataReader["contentlist"].ToString().Replace("|br", "<br>");
                                    strContent = strContent.Replace("[#Comm_Content]", ContentList);
                                }
                                //会员名称
                                if (strContent.IndexOf("[#Member_Name]") > -1)
                                {
                                    strContent = strContent.Replace("[#Member_Name]", memberName);
                                }
                                //会员等级
                                if (strContent.IndexOf("[#Member_Level]") > -1)
                                {
                                    strContent = strContent.Replace("[#Member_Level]", memberLevel);
                                }
                                //商品标签
                                if (strContent.IndexOf("[#Comm_Lable]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comm_Lable]", dataPage.DataReader["tag"].ToString());
                                }
                                //回复按钮
                                if (strContent.IndexOf("[#Comm_ReplyButton]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comm_ReplyButton]", "<input type=\"button\" name=\"button\" id=\"button\" value=\"回复(0)\"  onclick=\"javascript:window.location.href='"  + YXShop.TemplateAction.Common.UrlStr("commentreply", "q_commentid=" + ComID + "", "comment/") + "'\"/>");
                                }
                                //回复链接
                                if (strContent.IndexOf("[#Comm_ReplyLink]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comm_ReplyLink]", "" + YXShop.TemplateAction.Common.UrlStr("commentreply", "q_commentid=" + ComID + "", "comment/") + "");
                                }
                                //鲜花数量
                                if (strContent.IndexOf("[#Comm_Flowers]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comm_Flowers]", "<span class=\"commentFlower\" id='operate" + ComID + "' onClick=\"operateComment('flower'," + ComID + "," + dataPage.DataReader["flowernum"].ToString() + ")\">鲜花[<span class=\"flowerCount\">" + dataPage.DataReader["flowernum"].ToString() + "</span>次]</span>");
                                }
                                //支持数量
                                if (strContent.IndexOf("[#Comm_Support]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comm_Support]", "<span class=\"commentStand\" id=\"stand" + ComID + "\" onClick=\"operateComment('stand'," + ComID + "," + dataPage.DataReader["supportnum"].ToString() + ")\">支持[<span class=\"standCount\">" + dataPage.DataReader["supportnum"].ToString() + "</span>次]</span>");
                                }
                                //反对
                                if (strContent.IndexOf("[#Comm_Combat]") > -1)
                                {
                                    strContent = strContent.Replace("[#Comm_Combat]", "<span class=\"commentOppose\" id=\"oppose" + ComID + "\" onClick=\"operateComment('oppose'," + ComID + "," + dataPage.DataReader["againstnum"].ToString() + ")\">反对[<span class=\"opposeCount\">" + dataPage.DataReader["againstnum"].ToString() + "</span>次]</span>");
                                }
                                if (!string.IsNullOrEmpty(strContent))
                                {
                                    shtml.Append(strContent);
                                }
                                if (str_TempletLayout == "1")
                                {
                                    if (count % Convert.ToInt32(str_ShowColumns) == 0)
                                    {
                                        shtml.Append("</tr><tr>");
                                    }

                                }
                            }
                            //分页
                            if (str_WhetherPagination == "0")
                            {
                                shtml.Append(dataPage.ReceptionToolBar);
                            }
                            mystyle = shtml.ToString();
                        }
                        else
                        {
                            mystyle = "未找到点评信息";
                        }
                    }
                    
                    break;
            }

            return mystyle;
        }
    }
}

